package com.cqupt.slewing

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import com.cqupt.slewing.room.Item
import com.cqupt.slewing.room.ItemViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class HomeFragment : Fragment() {
    private lateinit var myViewModel: MyViewModel
    private lateinit var itemViewModel: ItemViewModel

    private lateinit var textViewWelcome: TextView
    private lateinit var imageViewNotification: ImageView
    private lateinit var editTextMac: EditText
    private lateinit var buttonTrack: Button


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_home, container, false)
        textViewWelcome = view.findViewById(R.id.textViewWelcome)
        imageViewNotification = view.findViewById(R.id.imageViewNotification)
        editTextMac = view.findViewById(R.id.editTextMac)
        buttonTrack = view.findViewById(R.id.buttonTrack)

        val shp = requireActivity().getSharedPreferences("MyData", Context.MODE_PRIVATE)
        textViewWelcome.append(shp.getString("username", "User"))

        imageViewNotification.setOnClickListener {
            val controller = Navigation.findNavController(it)
            controller.navigate(R.id.action_homeFragment_to_notificationFragment)
        }

        // debug
        editTextMac.setText("F29c1b7db427")
//        editTextMac.setText("f23a94c5a9f0")
        return view
    }


    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        myViewModel = ViewModelProvider(requireActivity())[MyViewModel::class.java]
        itemViewModel = ViewModelProvider(requireActivity())[ItemViewModel::class.java]

        val bottomNavigationView: BottomNavigationView =
            requireActivity().findViewById(R.id.bottom_nav_view)
        bottomNavigationView.visibility = View.VISIBLE //显示底部导航栏


        buttonTrack.setOnClickListener {
            val macID = editTextMac.text.toString()
            if (macID.length != 12) {
                Toast.makeText(
                    requireActivity(),
                    "The MAC address format is incorrect",
                    Toast.LENGTH_SHORT
                ).show()
            } else {
                lifecycleScope.launch(Dispatchers.IO) {
                    if (itemViewModel.getItemByMac(macID) == null) {
                        val item = Item(macID)
                        itemViewModel.insert(item)
                    }
                    myViewModel.bindDevice(macID)
                }
                val bundle = bundleOf("macID" to macID)
                val controller = Navigation.findNavController(it)
                controller.navigate(R.id.action_homeFragment_to_trackFragment, bundle)

            }
        }
    }
}